package com.leather.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.common.entity.Goods;
import com.leather.common.entity.GoodsCar;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.CheckKit;
import com.leather.market.config.MQConfig;
import com.leather.market.mapper.GoodsCarMapper;
import com.leather.market.mapper.GoodsMapper;
import com.leather.market.service.GoodsService;
import com.leather.market.vo.GoodsQueryVo;
import com.leather.market.vo.GoodsVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsCarMapper goodsCarMapper;


    @Override
    public Result addGoods(GoodsVo goodsVo) {
        Result result = goodsVo.validate();
        if (!result.isSuccess()) {
            return result;
        }
        Goods goods = (Goods) result.getData();
        CheckKit.check(save(goods), CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result queryIndex(int pageId) {
        List<GoodsQueryVo> lists = goodsMapper.index(pageId);
        return Result.success(lists);
    }

    @Override
    public Result userCollection(int userId) {
        List<GoodsQueryVo> lists = goodsMapper.userCollection(userId);
        return Result.success(lists);
    }

    @Override
    public Result userCenter(int userId) {
        List<Goods> lists = query().eq("user_id", userId).ne("flag",2).list();
        return Result.success(lists);
    }

    @Override
    public Result setFlag(int goodsId, int userId) {
        CheckKit.check(goodsId > 0, CodeMsg.ERROR_GOODS_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        boolean result = update().eq("user_id", userId).eq("id", goodsId).set("flag", 1).update();
        CheckKit.check(result, CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result search(String searchValue) {
        List<GoodsQueryVo> lists = goodsMapper.search(searchValue);
        return Result.success(lists);
    }

    @Override
    public Result addCar(GoodsCar goodsCar) {
        goodsCar.setId(0);
        int count = goodsCarMapper.insert(goodsCar);
        CheckKit.check(count > 0, CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result removeCar(GoodsCar goodsCar) {
        return getResult("remove", goodsCar);
    }

    @Override
    public Result judgeIsAdd(GoodsCar goodsCar) {
        return getResult("judge", goodsCar);
    }

    @Override
    public Result delete(int goodsId, int userId) {
        CheckKit.check(goodsId > 0, CodeMsg.ERROR_GOODS_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        boolean result = update().eq("user_id", userId).eq("id", goodsId).set("flag", 2).update();
        CheckKit.check(result, CodeMsg.SERVER_ERROR);
        Goods goods = getById(goodsId);
        amqpTemplate.convertAndSend(MQConfig.QUAN_QUENE,goods.getGoodsImgUrl());
        return Result.success("ok");
    }

    @Override
    public Result updatePrice(int userId, int goodsId, double price) {
        CheckKit.check(goodsId > 0, CodeMsg.ERROR_GOODS_ID);
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        CheckKit.check(price>0,CodeMsg.EMPTY_GOODS_PRICE);
        boolean result = update().eq("user_id", userId).eq("id", goodsId).set("goods_price", price).update();
        CheckKit.check(result,CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    private Result getResult(String ywgjz, GoodsCar goodsCar) {
        QueryWrapper<GoodsCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsCar.getGoodsId()).eq("user_id", goodsCar.getUserId());
        int count;
        if ("judge".equals(ywgjz)) {
            count = goodsCarMapper.selectCount(queryWrapper);
            return Result.success(count>0);
        } else {
            count = goodsCarMapper.delete(queryWrapper);
            CheckKit.check(count > 0, CodeMsg.SERVER_ERROR);
            return Result.success("ok");
        }
    }

    @Override
    public Result getByType(int typeId) {
        List<GoodsQueryVo> types = goodsMapper.getByType(typeId);
        return Result.success(types);
    }
}
